package routers

import (
	"gitee.com/tsingeye/FreeEhome/controllers"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/plugins/cors"
)

func init() {
	//参数说明
	//AllowAllOrigins 允许全部来源设置为true则所有域名都可以访问本网站接口，可以将此配置换成为AllowOrigins：[“允许访问的域名”]
	//AllowMethods :允许的请求类型
	//AllowHeaders：允许的头部信息
	//ExposeHeaders：允许暴露的头信息
	//AllowCredentials：如果设置，允许共享AuthTuffic证书，如Cookie
	//实现服务端允许跨域
	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
		AllowAllOrigins:  true,
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
		AllowHeaders:     []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type", "Token"},
		ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type", "Token"},
		AllowCredentials: true,
	}))

	ns := beego.NewNamespace("/api/v1",
		//系统接口
		beego.NSNamespace("/system",
			//登录
			beego.NSRouter("/login", &controllers.SystemController{}, "post:Login"),
			//登出
			beego.NSRouter("/logout", &controllers.SystemController{}, "get:Logout"),
			//获取系统信息：CPU、网络、内存等
			beego.NSRouter("/info", &controllers.SystemController{}, "get:SystemInfo"),
		),

		//设备接口
		//查询设备列表
		beego.NSRouter("/devices", &controllers.DeviceController{}, "get:DeviceList"),
		//查询指定设备下的通道列表
		beego.NSRouter("/devices/:deviceID/channels", &controllers.DeviceController{}, "get:ChannelList"),
		//更新通道
		beego.NSRouter("/channels/:channelID", &controllers.DeviceController{}, "patch:UpdateChannel"),
		//云台控制
		beego.NSRouter("/channels/:channelID/ptzControl", &controllers.DeviceController{}, "get:PtzControl"),

		//实时直播接口
		//实时直播启动
		beego.NSRouter("/channels/:channelID/inviteStream", &controllers.StreamController{}, "get:InviteStream"),
		beego.NSRouter("/channels/:channelID/getSnap", &controllers.StreamController{}, "get:GetSnap"),
		//录像接口
		//指定通道录像文件查询
		beego.NSRouter("/channels/:channelID/queryRecordedFiles", &controllers.RecordController{}, "get:QueryRecordedFiles"),
		//指定通道录像文件播放
		beego.NSRouter("/channels/:channelID/uploadFile", &controllers.RecordController{}, "get:UploadFile"),

		//文件下载接口
		//文件路径列表
		beego.NSRouter("/file/list", &controllers.FileUploadController{}, "get:FileList"),
		//文件下载
		beego.NSRouter("/file/download", &controllers.FileUploadController{}, "get:FileDownload"),
	)
	beego.AddNamespace(ns)

	//Hook接口
	nsHook := beego.NewNamespace("/index",
		beego.NSNamespace("/hook",
			beego.NSRouter("/on_stream_none_reader", &controllers.HookController{}, "post:OnStreamNoneReader"),
			beego.NSRouter("/on_publish", &controllers.HookController{}, "post:OnPublish"),
			beego.NSRouter("/on_record_mp4", &controllers.HookController{}, "post:OnRecordMP4"),
			beego.NSRouter("/on_http_access", &controllers.HookController{}, "post:OnHttpAccess"),
			beego.NSRouter("/on_stream_not_found", &controllers.HookController{}, "post:OnStreamNotFound"),
			beego.NSRouter("/on_play", &controllers.HookController{}, "post:OnPlay"),
		),
	)
	beego.AddNamespace(nsHook)
}
